<template>
  <div class="login">
    <div class="box">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item prop="username" class="el_form">
          <span slot="label" style="color: #010712">
            <span>用户名</span>
          </span>
          <el-input
            v-model="ruleForm.username"
            type="username"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item prop="password" class="el_form">
          <span slot="label" style="color: #010712">
            <span>密码</span>
          </span>
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm(ruleFormRef)"
            class="login_btn"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { login } from "@/api/Users";
import router from "@/router";
import { useStore } from "vuex";
const ruleFormRef = ref<FormInstance>();

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入用户名"));
  } else {
    callback();
  }
};

const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  username: "admin",
  password: "123456",
});

const rules = reactive({
  username: [{ validator: validatePass, trigger: "blur" }],
  password: [{ validator: validatePass2, trigger: "blur" }],
});

// 获取token
const store = useStore();
const submitForm = async (formEl: FormInstance | undefined) => {
  let res = await login({
    username: ruleForm.username,
    password: ruleForm.password,
  });
  console.log(res, "res");

  if (res.data.meta.status === 200) {
    store.commit("user/SET_TOKEN", res.data.data.token); //存入token
    store.commit("user/SET_USER", res.data.data); //存入登录用户信息
    sessionStorage.setItem("token", res.data.data.token);
    router.push("/");
  }
  console.log("store", store.state.user.token);
  console.log("store2", store.state.user.user);
  console.log(res);
  console.log(res.data.meta);
  console.log(ruleForm);

  // if (!formEl) return;
  // formEl.validate((valid) => {
  //   if (valid) {
  //     console.log("登录!");
  //   } else {
  //     console.log(ruleForm, "登陆失败!");
  //     return false;
  //   }
  // });
};
</script>

<style scoped lang="scss">
.login {
  background-image: url("../../../assets/2.webp");
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh;
  width: 100vw;
  opacity: 0.7;
  display: flex;
  justify-content: center;
  align-items: center;
}

.box {
  width: 350px;
  height: 400px;
  text-align: center;
  margin: 0 auto;
  padding-top: 50px;
  // margin-top: 10%;
  background-color: rgba(166, 183, 206, 0.5);
  border-radius: 15px;
}
.demo-ruleForm {
  width: 60%;
  margin: 22px auto;

  .el_form {
    margin-bottom: 25px;
    margin-left: -100px;
    width: 300px;
  }

  .label {
    display: block;
  }

  .login_btn {
    display: block;
    width: 300px;
    margin-top: 10px;
    margin-left: -100px;
    margin-right: 10px;
  }
}
</style>
